<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="closeFn">
    <el-form ref="form" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <!-- <el-input v-model="formData.timeOfEntry" /> -->
        <el-date-picker
          v-model="formData.timeOfEntry"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment">
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" @focus="showDepts=true" />
        <!-- 部门列表 -->
        <el-tree
          v-if="showDepts"
          :data="depts"
          :props="{label: 'name'}"
          @node-click="selectDept"
        />
      </el-form-item>
      <el-form-item label="转正时间">
        <!-- <el-input v-model="formData.correctionTime" /> -->
        <el-date-picker
          v-model="formData.correctionTime"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
    </el-form>
    <!-- footer: 插槽名，显示在弹窗的最底下 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-button @click="submit">提交</el-button>
        <el-button @click="closeFn">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>
<script>
import employees from '@/api/constant/employees'
import { getDepartments } from '@/api/department.js'
import { addEmployee } from '@/api/employees.js'
import { listToTree } from '@/utils/index.js'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        username: '', // 姓名
        mobile: '', // 手机号
        formOfEmployment: null, // 数字类型
        workNumber: '', // 工号
        departmentName: '', // 部门名称
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空！', trigger: 'blur' },
          { min: 1, max: 4, message: '姓名长度1到4位！', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号码不能为空！', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号码格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空！', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空！', trigger: 'blur' }
        ],
        departmentName: [
          // change 表示数据发生变化时校验
          { required: true, message: '部门名称不能为空！', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空！', trigger: 'blur' }
        ]
      },
      hireType: employees.hireType, // 聘用形式
      depts: [], // 部门列表
      showDepts: false // 默认隐藏部门列表
    }
  },
  async created() {
    const res = await getDepartments()
    this.depts = listToTree(res.data.data.depts, '')
  },
  methods: {
    // 提交 新增
    submit() {
      // 表单校验
      this.$refs.form.validate(async bool => {
        if (bool) {
          // 校验通过 调用接口
          await addEmployee(this.formData)
          //   关闭弹窗
          this.closeFn()
          //   刷新员工列表数据
          this.$emit('updateEvent')
        }
      })
    },
    // 选中部门名称
    selectDept(data) {
      this.formData.departmentName = data.name
      this.showDepts = false
    },
    // 关闭弹窗
    closeFn() {
      // 关闭弹窗
      this.$emit('update:showDialog', false)
      //   清空数据
      this.formData = {
        username: '', // 姓名
        mobile: '', // 手机号
        formOfEmployment: null, // 数字类型
        workNumber: '', // 工号
        departmentName: '', // 部门名称
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      }
      // 清空校验规则
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
